home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue40 / Keystrok / KeysU.pas next >
Encoding:
Pascal/Delphi Source File  |  1998-10-05  |  2.0 KB  |  88 lines

  1. unit KeysU;
  2.  
  3. interface
  4.  
  5. procedure PressKey(Key: Char);
  6. procedure ReleaseKey(Key: Char);
  7. procedure SendKeys(const Keys: String);
  8.  
  9. const
  10.   SnapShotWholeScreen: Boolean = False;
  11.  
  12. implementation
  13.  
  14. uses
  15.   WinTypes, WinProcs, Forms, SysUtils;
  16.  
  17. const
  18.   KeyEventF_KeyDown = 0;
  19. {$ifndef WIN32}
  20.   KeyEventF_KeyUp = $80; {It changes to 2 in Win32}
  21.  
  22. procedure Keybd_Event; far; external 'USER' index 289;
  23.  
  24. procedure PostVirtualKeyEvent(vk: Word; fUp: Boolean);
  25. var
  26.   AXReg, BXReg: WordRec;
  27. const
  28.   ButtonUp: array[Boolean] of Byte = (KeyEventF_KeyDown, KeyEventF_KeyUp);
  29. begin
  30.   AXReg.Hi := ButtonUp[fUp];
  31.   AXReg.Lo := vk;
  32.   BXReg.Hi := 0; { not an extended scan code }
  33.   { Special processing for the PrintScreen key. }
  34.   { If scan code is set to 1 it copies entire }
  35.   { screen. If set to 0 it copies active window. }
  36.   if vk = vk_SnapShot then
  37.     BXReg.Lo := Byte(SnapShotWholeScreen)
  38.   else
  39.     BXReg.Lo := MapVirtualKey(vk, 0);
  40.   asm
  41.     mov ax, AXReg
  42.     mov bx, BXReg
  43.     call Keybd_Event
  44.   end;
  45. end;
  46. {$else}
  47. procedure PostVirtualKeyEvent(vk: Word; fUp: Boolean);
  48. var
  49.   ScanCode: Byte;
  50. const
  51.   ButtonUp: array[Boolean] of Byte = (KeyEventF_KeyDown, KeyEventF_KeyUp);
  52. begin
  53.   if vk = vk_SnapShot then
  54.     { Special processing for the PrintScreen key. }
  55.     { If scan code is set to 1 it copies entire }
  56.     { screen. If set to 0 it copies active window. }
  57.     ScanCode := Byte(SnapShotWholeScreen)
  58.   else
  59.     ScanCode := MapVirtualKey(vk, 0);
  60.   Keybd_Event(vk, ScanCode, ButtonUp[fUp], 0);
  61. end;
  62. {$endif}
  63.  
  64. procedure PressKey(Key: Char);
  65. begin
  66.   PostVirtualKeyEvent(Ord(Key), False)
  67. end;
  68.  
  69. procedure ReleaseKey(Key: Char);
  70. begin
  71.   PostVirtualKeyEvent(Ord(Key), True)
  72. end;
  73.  
  74. procedure SendKeys(const Keys: String);
  75. var
  76.   Loop: Byte;
  77. begin
  78.   for Loop := 1 to Length(Keys) do
  79.   begin
  80.     PostVirtualKeyEvent(Ord(Keys[Loop]), False); { Press key }
  81.     PostVirtualKeyEvent(Ord(Keys[Loop]), True);  { Release key }
  82.   end;
  83.   { Let the keys be processed }
  84.   Application.ProcessMessages;
  85. end;
  86.  
  87. end.
  88.